package com.mmlc.meimei.lichengkongjian.mine;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.mmlc.businessutils.PullToRefreshUtils;
import com.mmlc.inter.InterActivity;
import com.mmlc.meimei.lichengkongjian.Adapter.RefundListAdapter;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.been.RefundListBean;
import com.mmlc.network.filter.RefundListRequestFilter;
import com.mmlc.network.responsebean.RefundListResponseBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class WaitRefundActivity extends InterActivity {

    @InjectView(R.id.tv_title)
    TextView tvTitle;
    @InjectView(R.id.iv_back)
    ImageView ivBack;
    @InjectView(R.id.iv_set)
    ImageView ivSet;
    @InjectView(R.id.pull_refresh_list)
    PullToRefreshListView pullRefreshList;
    @InjectView(R.id.rootView)
    RelativeLayout rootView;

    @Override
    protected int initContentView() {
        return R.layout.activity_waitreceivedshop;
    }

    @Override
    protected void initView() {
        ButterKnife.inject(this);

        processData();
    }
    
    
    private void processData() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ivSet.setVisibility(View.GONE);
        tvTitle.setText("退款");

        initPullToRefresh();
        requestDataForList();

    }

    PullToRefreshUtils pullUtils;
    private void initPullToRefresh() {
        pullRefreshList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                position = position -1;
                Intent intent = new Intent(activity, RefunddetailActivity.class);
                intent.putExtra("order_id",item.get(position).orderId);
                activity.startActivity(intent);
            }
        });

        pullUtils =  new PullToRefreshUtils(pullRefreshList, new PullToRefreshUtils.RefreshListener<RefundListBean>() {


            @Override
            public boolean compareFirstItem(RefundListBean adapterFirstItem) {
                String id = currResult.getData().get(0).getId();
                String orderId = adapterFirstItem.orderId;
                return orderId.equals(id);
            }

            @Override
            public void processList(List<RefundListBean> list) {
                processAdapter();
            }

            @Override
            public void refresh(boolean isPull) {
                requestDataForList();
            }

            @Override
            public List<RefundListBean> getAdapterList() {
                return item;
            }

            @Override
            public List<RefundListBean> getRequestList() {
                return filter.uiBeanList;

            }
        });


    }

    public int pageNow = 0;
    public int pageSize = 10;
    List<RefundListBean> item = new ArrayList<>();
    RefundListRequestFilter filter;


    public RefundListResponseBean currResult;
    public boolean isExcuteOne = true;
    private void requestDataForList() {

        if (filter == null) {

            filter = new RefundListRequestFilter(WaitRefundActivity.this);
            filter.requestBean.method = "refundlist";
        }

        filter.requestBean.pageNow = pullUtils.currPageIndex;
        filter.requestBean.pageSize = pageSize;
        filter.offerErrorParams(rootView);
        if(isExcuteOne){
            isExcuteOne = false;
            filter.isTransparence = false;
        }else{
            filter.isTransparence = true;
        }
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<RefundListResponseBean>() {

            @Override
            public void onSuccess(RefundListResponseBean result) {
                currResult = result;

                pullUtils.recordRequest(filter,true);
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                pullUtils.recordRequest(null,false);
            }
        });
    }


    RefundListAdapter adapter;
    private void processAdapter( ) {
        if (adapter == null) {
            adapter = new RefundListAdapter(pullRefreshList, item, WaitRefundActivity.this);
            pullRefreshList.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void initEvent() {

    }


}
